package net.lzzy.practices.service;

import net.lzzy.practices.entities.User;
import net.lzzy.practices.repositories.UserProjection;
import net.lzzy.practices.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * @author 渊何
 */
@Service
public class UserServiceImpl implements UserService{
    private final UserRepository repository;

    @Autowired
    public UserServiceImpl(UserRepository repository) {
        this.repository = repository;
    }

    @Override
    public User check(String userName, String password) {
        return repository.findUserByUserNameAndPassword(userName, password);
    }

    @Override
    public boolean isUserNameOccupied(String userName) {
        return repository.countByUserName(userName) > 0;
    }

    @Override
    public User register(User user) {
        return repository.save(user);
    }

    @Override
    public List<User> get() {
        return repository.findAllByOrderByUpdateTimeDesc();
    }

    @Override
    public User getById(UUID id){
        return repository.getById(id);
    }

    @Override
    public List<User> get(int page, int size) {
        return repository.findAllByOrderByUpdateTimeDesc(PageRequest.of(page,size));
    }

    @Override
    public Integer count() {
        return repository.countAllBy();
    }

    @Override
    public List<User> search(String kw) {
        return repository.findByQuery(kw);
    }

    @Override
    public List<User> getApplying(boolean applying) {
        return repository.findUsersByApplyTeacherOrderByUpdateTimeDesc(applying);
    }

    @Override
    public User approveTeacher(UUID id) {
        //findById是eager,getById是延迟查询
        User user = repository.findById(id).orElse(null);
        if (user != null){
            user.setType(User.TYPE_TEACHER);
            user.setApplyTeacher(false);
            return repository.save(user);
        }
        return null;
    }

    @Override
    public User declineTeacher(UUID id) {
        User user = repository.findById(id).orElse(null);
        if (user != null){
            user.setApplyTeacher(false);
            return repository.save(user);
        }
        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public User banUser(UUID id) {
        User user = repository.findById(id).orElse(null);
        if (user != null){
            user.setType(User.TYPE_BANNED);
            user.setApplyTeacher(false);
            return repository.save(user);
        }
        return null;
    }

    @Override
    public String getSalt(String userName) {
        List<UserProjection> salts = repository.findUsersByUserName(userName);
        if (salts == null || salts.size() == 0){
            return "";
        }
        UserProjection projection = salts.get(0);
        if (projection == null){
            return "";
        }
        String salt = projection.getSalt();
        return salt == null ? "" : salt;
    }
}
